﻿Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO

Public Class ResultScreen

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Application.Exit() 'termatizei i efarmogi


    End Sub

    Private Sub NewSearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewSearchButton.Click


        Me.Visible = False 'kleinei auti kai anoigei i proigoumeni
        DatabaseSearchScreen.Visible = True

        'clear ama dn einai adeio
        'katharismos pediwn 1hs formas

        'ResultBox.Items.Clear() na to tsekaroume na douleuei swsta


        DatabaseSearchScreen.PinakesCbox.Text = ""
        DatabaseSearchScreen.PinakesCbox2.Text = ""
        DatabaseSearchScreen.PinakesCbox3.Text = ""

        DatabaseSearchScreen.PediaEmfCbox11.Text = ""
        DatabaseSearchScreen.PediaEmfCbox12.Text = ""
        DatabaseSearchScreen.PediaEmfCbox21.Text = ""
        DatabaseSearchScreen.PediaEmfCbox22.Text = ""
        DatabaseSearchScreen.PediaEmfCbox31.Text = ""
        DatabaseSearchScreen.PediaEmfCbox32.Text = ""

        DatabaseSearchScreen.PediaCbox11.Text = ""
        DatabaseSearchScreen.PediaCbox12.Text = ""
        DatabaseSearchScreen.PediaCbox21.Text = ""
        DatabaseSearchScreen.PediaCbox22.Text = ""
        DatabaseSearchScreen.PediaCbox31.Text = ""
        DatabaseSearchScreen.PediaCbox32.Text = ""

        DatabaseSearchScreen.EpilPraxisCbox11.Text = ""
        DatabaseSearchScreen.EpilPraxisCbox12.Text = ""
        DatabaseSearchScreen.EpilPraxisCbox21.Text = ""
        DatabaseSearchScreen.EpilPraxisCbox22.Text = ""
        DatabaseSearchScreen.EpilPraxisCbox31.Text = ""
        DatabaseSearchScreen.EpilPraxisCbox32.Text = ""

        DatabaseSearchScreen.EpilogiTbox11.Text = ""
        DatabaseSearchScreen.EpilogiTbox12.Text = ""
        DatabaseSearchScreen.EpilogiTbox21.Text = ""
        DatabaseSearchScreen.EpilogiTbox22.Text = ""
        DatabaseSearchScreen.EpilogiTbox31.Text = ""
        DatabaseSearchScreen.EpilogiTbox32.Text = ""

        DatabaseSearchScreen.AndOrCbox1.Text = ""
        DatabaseSearchScreen.AndOrCbox2.Text = ""
        DatabaseSearchScreen.AndOrCbox3.Text = ""



        DatabaseSearchScreen.Pin2Checkbox.Checked = False
        DatabaseSearchScreen.Pin3Checkbox.Checked = False
        DatabaseSearchScreen.PediaEmf1Checkbox.Checked = False
        DatabaseSearchScreen.PediaEmf2Checkbox.Checked = False
        DatabaseSearchScreen.PediaEmf3Checkbox.Checked = False

        DatabaseSearchScreen.PinakesCbox2.Enabled = False
        DatabaseSearchScreen.PinakesCbox3.Enabled = False

    End Sub

    '***************
    'EXPORT TO EXCEL
    '***************
    Private Sub SaveAsTextToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportExcel.Click
        Dim rowsTotal, colsTotal As Short
        Dim I, j, iC As Short
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
        Dim xlApp As New Excel.Application
        Try
            Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
            Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
            xlApp.Visible = True
            rowsTotal = GridView.RowCount - 1
            colsTotal = GridView.Columns.Count - 1
            With excelWorksheet
                .Cells.Select()
                .Cells.Delete()
                For iC = 0 To colsTotal
                    .Cells(1, iC + 1).Value = GridView.Columns(iC).HeaderText
                Next
                For I = 0 To rowsTotal
                    For j = 0 To colsTotal
                        .Cells(I + 2, j + 1).value = GridView.Rows(I).Cells(j).Value
                    Next j
                Next I
                .Rows("1:1").Font.FontStyle = "Bold"
                .Rows("1:1").Font.Size = 10
                .Cells.Columns.AutoFit()
                .Cells.Select()
                .Cells.EntireColumn.AutoFit()
                .Cells(1, 1).Select()
            End With
        Catch ex As Exception
            MsgBox("Export Excel Error " & ex.Message)
        Finally
            'RELEASE ALLOACTED RESOURCES
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            xlApp = Nothing
        End Try
    End Sub

    '**************
    'EXPORT TO TXT
    '**************
    Private Sub ExportToTxtToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportToTxtToolStripMenuItem.Click
        If SaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Try
                Using fs As New FileStream(SaveFileDialog.FileName, FileMode.Create)
                    Dim numCols As Integer = GridView.ColumnCount
                    Dim numRows As Integer = GridView.RowCount - 1
                    'Dim strDestinationFile As String = "C:\Users\Admin\Desktop\datagridmessage.txt"
                    Dim tw As TextWriter = New StreamWriter(fs)

                    'Grafw to SQL Querry
                    For Each item As Object In ResultBox.Items
                        tw.Write(item & vbCrLf & vbCrLf)
                    Next

                    'Grafw to onoma tou pediou
                    For count As Integer = 0 To numCols - 1
                        tw.Write(GridView.Columns(count).HeaderText)
                        If (count <> numCols - 1) Then
                            tw.Write(", ")
                        End If
                    Next

                    'Grafw ta pedia tou GridView
                    tw.WriteLine()
                    For count As Integer = 0 To numRows - 1
                        For count2 As Integer = 0 To numCols - 1
                            tw.Write(GridView.Rows(count).Cells(count2).Value)
                            If (count2 <> numCols) Then
                                tw.Write(", ")
                            End If
                        Next
                        tw.WriteLine()
                    Next
                    tw.Close()
                    MessageBox.Show("File Saved as Txt")
                End Using

            Catch ex As Exception
                MessageBox.Show(ex.Message, "File Error")
            End Try

        End If
    End Sub
End Class